<template>
    <div class="drugTab-view">

      <div class="drugTab-content">
        <div class="tab-top">

          <div class="drug top-item">
            药品名称
          </div>

          <div class="specifications top-item">
            规格
          </div>

          <div class="manufacturer top-item">
            厂家
          </div>

          <div class="total top-item">
            单价
          </div>

          <div class="number top-item">
            数量
          </div>

          <div class="price top-item">
            现价
          </div>

          <div class="total top-item">
            总价
          </div>

          <div class="delete"></div>

        </div>

        <drug-tab-item v-for="(item,index) in list" :key="item.id" :data="item" :index="index" :total="list.length" @delete-item="deleteItem" @add-item="addClick" @amount-change="amountChange"></drug-tab-item>
      </div>


      <div class="addDrug">
        <a @click="addClick">添加药品</a>
      </div>

    </div>
</template>

<script>
  import drugTabItem from "./drugTabItem";

    export default {
        name: "drugTab",
        components:{drugTabItem},
        data:function () {
            return{
                number:1,
                list:[{number:0,price:0,amount:0,id:1}],
            }
        },
        methods:{
            deleteItem:function (index) {
                this.list.splice(index,1);
                this.$emit("delete")
                this.amountChange();
            },
            addClick:function () {
                this.number++;
                this.list.push({price:0,amount:0,number:0,id:this.number});
            },
            validate:function () {
                let list = [];
                for (let i = 0; i < this.list.length; i++){
                    if ((this.list[i].product_id > 0 && this.list[i].product_name != "") && this.list[i].number > 0){
                        this.list[i].price = Number(this.list[i].price)
                        this.list[i].product_status = 2
                        list.push(this.list[i]);
                    }
                }
                if (list.length == 0){
                    this.$message.error('请添加药品');
                    return null
                }
                return list;
            },
            amountChange:function () {
                let total = 0;
                for (let i = 0; i < this.list.length; i++){
                    if ((this.list[i].product_name && this.list[i].product_name != '') && this.list[i].amount){
                        total += this.list[i].amount;
                    }
                }
                this.$emit("total-change",total)
            },
            clearData:function () {
                this.list = [{number:0,price:0,amount:0}]
            }
        }
    }
</script>

<style lang="scss" scoped>
  @import "@/assets/scss/payOrDrug/drugTab.scss";
</style>
